import { ElMessage } from "element-plus"
export default defineNuxtPlugin(nuxtApp => {
  nuxtApp.provide('openUrl', (url)=>{
    // console.log('openUrl',url)
    if(typeof url !== 'string'){
      if(url.productId){
        navigateTo(`/product/detail/${url.productId}`)
      }else if(url.articleId){
        navigateTo(`/news/detail/${url.articleId}`)
      }else if(url.videoId){
        navigateTo(`/video/detail/${url.videoId}`)
      }else if(url.my){
        navigateTo(`/my/detail`)
      }else if(url.company){
        navigateTo(`/company/detail`)
      }else if(url.connect){
        navigateTo(`/connect/detail`)
      }else if(url.index){
        navigateTo(`/`)
      }
    }else{
      window.open(url)
    }
    
  }),
  nuxtApp.provide('getValByKey',()=>{
    console.log('更换')
  }),
  nuxtApp.provide('getCurrentTimePlus',()=>{
    let now=new Date()
    let year=now.getFullYear()
    let month=now.getMonth()+1
    let day=now.getDay()
    let hour=now.getHours()
    let minute=now.getMinutes()
    let second=now.getSeconds()

    if(month<12){
      month+=1
    }
    

    if(month<10){
      month='0'+month
    }
    if(day<10){
      day='0'+day
    }
    if(hour<10){
      hour='0'+hour
    }
    if(minute<10){
      minute='0'+minute
    }
    if(second<10){
      second='0'+second
    }
    // console.log(year+'-'+month+'-'+day+' '+hour+':'+minute+':'+second)
        return year+'-'+month+'-'+day+' '+hour+':'+minute+':'+second
  }),
  //毫秒 转 天/时/分/秒
  nuxtApp.provide('formatSecondToDate',(seconds)=>{
    let time={
      day:0,
      hour:0,
      minute:0,
      second:0
    }
    if (seconds>0) {
      time.day = Math.floor(seconds / (24 * 3600));
      time.hour = Math.floor(seconds / 3600 - time.day * 24);
      time.minute = Math.floor(seconds / 60 - time.day * 24 * 60 - time.hour * 60);
      time.second = Math.floor(seconds - time.day * 24 * 3600 - time.hour * 3600 - time.minute * 60);
    }
    time.day=time.day<10?'0'+time.day:time.day
    time.hour=time.hour<10?'0'+time.hour:time.hour
    time.minute=time.minute<10?'0'+time.minute:time.minute
    time.second=time.second<10?'0'+time.second:time.second

    return time
  }),
  //获取验证码动态参数
  /**随机八位字符串生成imgCodeToken
   * 随机生成[length]长度的[radix]（基数）进制数,radix默认62(10个数字26个大写26个小写)
   * 随机生成0-61的数值，8位，重复循环8次，重复概率为62的8次方
   * 如果怕重复，可调整length
   */
  nuxtApp.provide('getCodeParams',(length, radix)=>{
    let char = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
      let uuid = [];
      radix = radix || char.length;
      for (let i = 0; i < length; i++) {
          uuid[i] = char[0 | Math.random() * radix];
      }
      return uuid.join('');
  }),
  //顶部消息
  nuxtApp.provide('message',(type,msg)=>{
    // console.log('getImgCode',data)
    ElMessage({
      message: msg,
      type: type,
      plain: true,//plain 背景。
      showClose: false,
      center:true,//文字居中
    })
  })

})